<?php
// +----------------------------------------------------------------------
// | Name: keAdmin
// | Author King east To 1207877378@qq.com
// +----------------------------------------------------------------------

namespace app\admin\behavior;


use app\admin\model\AdminRule;
use ke\Controller;
use ke\Glo;
use think\Cache;

class Menu extends Controller
{
    // 取出菜单的路由
    private $route=[
        'Index/index'
    ];

    public function run($request)
    {
        $control=$request->controller() . '/' . $request->action();
        if(!in_array($control,$this->route)){
            return;
        }

        $menuKey='adminMenuList/'.$request->adminInfo['role_id'];
        $menuList=Cache::get($menuKey);
        if(!$menuList){
            $access=Glo::get('AccessAction');
            $menuList=AdminRule::field('id,parent_id,name AS url,title,icon')->where('hidden',0)->order('showorder','asc')->select()->toArray();
            $list=[];
            foreach ($menuList as $g){
                if($access->isAuth($g['url'])){
                    $g['url']=url($g['url']);
                    $list[]=$g;
                }
            }
            unset($menuList);
            $menuList=list_to_tree($list,'id','parent_id');
            Cache::set($menuKey,$menuList,86400*7);
        }


        $this->assign('menulist',$menuList);



    }

}